package com.FH.applyForm.officeServies.expenform_secondtype;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/21.
 */
@Stateless
public class ExpensecondDAOImpl extends AccessDB implements ExpensecondDAO {

    /**
     * 查询报销单费用
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getExpenformtypesInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from advance_types  where 1=1 ";

            String sql_w ="";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(page);
            if(page.equals("1")){
                sql_page = " limit 0,"+size+"";
            }else{
                sql_page = " limit "+((page_ok-1)*size_ok+1)+","+size+"";
            }

            if(json.has("advancetype_id")){
//                sql_w += " AND organization_id = "+json.getString("organization_id");
                sql_w += " AND advancetype_id ='" + json.getString("advancetype_id") + "'";
            }
            if(json.has("advancetypes_id")){
                sql_w += " AND advancetypes_id ='" + json.getString("advancetypes_id") + "'";
            }

            String sql_ok = sqlexit+sql_w ;//组合好的sql语句
            existName = getReportingToOne(sql_ok);//查询此数据是否存在

            List<String []> seresult = new ArrayList<String []>();

            List<ExpensecondShowDTO> result = new ArrayList<ExpensecondShowDTO>();

            if(!existName.equals("0")){
                String sql ="SELECT advancetypes_id,advancetypes_name,advancetypes_price,advancetypes_number,advancetype_id FROM advance_types WHERE 1=1 "+sql_w +sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++){

                    ExpensecondShowDTO advanceTypesShowDTO = new ExpensecondShowDTO();
                    String advancetypese_id = seresult.get(i)[1];
                    advanceTypesShowDTO.setAdvancetypes_id(advancetypese_id);
                    advanceTypesShowDTO.setAdvancetypes_name(seresult.get(i)[2]);
                    advanceTypesShowDTO.setAdvancetypes_price(seresult.get(i)[3]);
                    advanceTypesShowDTO.setAdvancetypes_number(seresult.get(i)[4]);
                    advanceTypesShowDTO.setAdvancetype_id(seresult.get(i)[5]);

                    result.add(advanceTypesShowDTO);


                }
            }


            if(seresult.size()!=0) {
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            }else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return resultDTO;

    }


    /**
     * 添加报销单费用
     * @param expensecondPullDTO
     * @return
     */
    @Override
    public ResultDTO addExpenformtypesInfo(ExpensecondPullDTO expensecondPullDTO) {
        int resultls=0;
        ResultDTO resultDTO=new ResultDTO();
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String advancetypes_name=expensecondPullDTO.getAdvancetypes_name();
        String sql="select * from advance_types where advancetypes_name='"+advancetypes_name+"'";
        String  sum=getReportingToOne(sql);
        if(!sum.equals("")){

            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("报销单信息已存在，请匁重复提交！");
            return resultDTO;
        }
        String sql1="";
        sql1="insert into advance_types (advancetypes_id,advancetypes_name,advancetypes_price,advancetypes_number,advancetype_id)" +
                "values('"+uuid+"','"+expensecondPullDTO.getAdvancetypes_name()+"','"+expensecondPullDTO.getAdvancetypes_price()+"','"+expensecondPullDTO.getAdvancetypes_number()+"','"+expensecondPullDTO.getAdvancetype_id()+"')";

        resultls=addReportingTo(sql1);
        if(resultls!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    /**
     * 修改报销单费用
     * @param expensecondPullDTO
     * @return
     */
    @Override
    public ResultDTO updateExpenformtypesInfo(ExpensecondPullDTO expensecondPullDTO) {
        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        //更新sql语句
        QUERY += "update advance_types set ";
        if(null!=expensecondPullDTO.getAdvancetypes_name()&&!"".equals(expensecondPullDTO.getAdvancetypes_name())){
            QUERY += " advancetypes_name = '" + expensecondPullDTO.getAdvancetypes_name() + "'";
        }
        if(null!=expensecondPullDTO.getAdvancetypes_price()&&!"".equals(expensecondPullDTO.getAdvancetypes_price())){
            QUERY += ", advancetypes_price = '" + expensecondPullDTO.getAdvancetypes_price() + "'";
        }
        if(null!=expensecondPullDTO.getAdvancetypes_number()&&!"".equals(expensecondPullDTO.getAdvancetypes_number())){
            QUERY += ", advancetypes_number = '" + expensecondPullDTO.getAdvancetypes_number() + "'";
        }


        QUERY += " where advancetypes_id = '" + expensecondPullDTO.getAdvancetypes_id() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    /**
     * 删除报销单费用
     * @param expensecondPullDTO
     * @return
     */
    @Override
    public ResultDTO deleteExpenformtypesInfo(ExpensecondPullDTO expensecondPullDTO) {
        String sql="select count(1) from advance_types WHERE advancetypes_id='"+expensecondPullDTO.getAdvancetypes_id()+"'";
        String existadvance= getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if(!existadvance.equals("0")){
            String QUERY = "";
            QUERY += "delete from advance_types  where advancetypes_id='"+expensecondPullDTO.getAdvancetypes_id()+"'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result!= 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
